<template>
  <Table
    :summaryMethod="summaryMethod"
    :pagination="false"
    showSummary
    filter
    v-bind="tableOpts"
    v-on="tableEvent"
  >
    <template #operator>
      <div class="operator-wrap"></div>
    </template>
  </Table>
</template>

<script>
import dayjs from "dayjs";
import tableMixins from "@/mixins/tableMixins.vue";
export default {
  name: "FinanceStatisticsIn",
  mixins: [tableMixins],
  components: {},
  data() {
    return {
      apiName: "financeFinanceStatistics",
      form: {
        date: {
          tag: "el-date-picker",
          label: "时间范围",
          value: [],
          props: {
            rangeSeparator: "至",
            startPlaceholder: "开始时间",
            endPlaceholder: "结束时间",
            type: "daterange",
          },
        },
      },
    };
  },
  mounted() {},
  methods: {
    summaryMethod(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (column.label == "金额") {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          } else {
            sums[index] = "-";
          }
        } else {
          sums[index] = "-";
        }
      });
      return sums;
    },
    formatQuery(opts) {
      const { date = [], ...params } = opts;
      if (date && date[0] && date[1]) {
        params.startTime= dayjs(date[0]).format("YYYY-MM-DD");
        params.endTime = dayjs(date[1]).format("YYYY-MM-DD");
      }
      params.type = "income";
      return params;
    },
  },
};
</script>

<style lang="less" scoped></style>
